package Controller;

import Data.*;
import MyInterfaces.MassageType;
import Service.Main;
import Service.SendMassage;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.Callback;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.ResourceBundle;

public class ChatMain {

 @FXML
 public ResourceBundle resources;
 private HashMap<ChatWithStage, ChatData> map=new HashMap<>();
    public Button addfriend;
    @FXML
 private URL location;
    @FXML
    private ImageView avatar;

    @FXML
    private ListView<GroupData> Group_list;
    private ObservableList<GroupData> Group_oblist;
 //   private ArrayList<GroupFriendData> Group_Arry;

 @FXML
 private Tab massagelist;

 @FXML
 private TreeView<?> friendtree;

 @FXML
 private Tab friendlist;

 private String id;
 @FXML
 private ListView<PrivateInformationData> UserInfolist;

 private Socket socket;

    @FXML
    private Label emailNumber;

    @FXML
    private Label NickName;

    @FXML
    private Button creatgroup;

    @FXML
    private Button addgroup;
    @FXML
    private Label userid;

    private String myid;
    private String myemail;



    private ManageMassageList MsgList=new ManageMassageList();

    public ManageMassageList getMsgList() {
        return MsgList;
    }

    private HashMap<String,PrivateInformationData> privateInformationDataHashMap=new HashMap<>();

    public PrivateInformationData getPID(String id)
    {
        return privateInformationDataHashMap.get(id);
    }
    public void putPID(String id,PrivateInformationData privateInformationData)
    {
        privateInformationDataHashMap.put(id,privateInformationData);
    }

    public PrivateInformationData getMydata()
    {
        return mydata;
    }
    public ChatData getChatDataByController(ChatWithStage chatWithStage)
    {
        return map.get(chatWithStage);
    }
    public void setSocket(Socket socket)
    {
        this.socket=socket;
    }
    public void RemoveChatData(ChatWithStage chatWithStage)
    {
        map.remove(chatWithStage);
    }
    public Socket getSocket()
    {
        return socket;
    }

    private PrivateInformationData mydata;
    public void changeMydata(PrivateInformationData mydata)
    {
        myid=mydata.getUserid();
        myemail=mydata.getEmailNumber();

        userid.setText("用户id:" + mydata.getUserid());
        //如果本次数据
        if(!mydata.getUserNickName().equals("")) NickName.setText("用户昵称:" + mydata.getUserNickName());
        else this.mydata.setUserNickName(mydata.getUserNickName());

        emailNumber.setText("用户邮箱:" + mydata.getEmailNumber());
        if(mydata.getAvatar()!=null)
        {
            //头像不为空
            Image avatarimage = new Image(new ByteArrayInputStream(mydata.getAvatar()));
            this.mydata.setAvatar(mydata.getAvatar());
            avatar.setFitHeight(100);
            avatar.setFitWidth(100);
            Circle circle=new Circle(50,50,50);
            avatar.setImage(avatarimage);
            avatar.setClip(circle);
        }
        else
        {
            //如果为空

        }
        id = mydata.getUserid();

        if(!mydata.getSex().equals("")) this.mydata.setSex(mydata.getSex());

        if(!mydata.getSaying().equals("")) this.mydata.setSaying(mydata.getSaying());

        this.mydata.setBirth(mydata.getBirth());
    }
    private ArrayList<PrivateInformationData> arry;

    public ArrayList<PrivateInformationData> getPrivateInformationArry()
    {
        return arry;
    }

    private ObservableList<PrivateInformationData> oblist;
    public void delFriend(String id)
    {
        ChatWithStage chatWithStage=Main.AllContronller.getChatWithStageById(id);
        if(chatWithStage!=null)
        {
            Main.AllContronller.RemoveChatWithStage(id);
            chatWithStage.CloseStage();
        }
        arry.remove(privateInformationDataHashMap.get(id));
        setChatStage(mydata,arry);
    }
    public void addFriend(PrivateInformationData fdata)
    {
        arry.remove(privateInformationDataHashMap.get(fdata.getUserid()));
        arry.add(fdata);
        System.out.println("添加成功"+arry.size());
        setChatStage(mydata,arry);
    }

    public void changeIsOnlie(PrivateInformationData privateInformationData)
    {
        for (int i = 0; i < arry.size(); i++)
        {
            if(arry.get(i).getUserid().equals(privateInformationData.getUserid()))
            {
                //如果是这个人，修改在线状态
                arry.get(i).setIsonline(privateInformationData.getIsonline());
                setChatStage(mydata,arry);
                break;
            }
        }
    }

    public void changeGroupData(GroupData groupData)
    {
        for (int i = 0; i < myGroupArry.size(); i++) {
            if(myGroupArry.get(i).getGroup_id().equals(groupData.getGroup_id()))
            {
                //找到了对应的群聊，开始改变，主要是显示的信息
                //头像，名称，简介

                if(groupData.getCreater_id()!=null)
                {
                    myGroupArry.get(i).setCreater_id(groupData.getCreater_id());
                }

                if(groupData.getGroup_name()!=null)
                {
                    myGroupArry.get(i).setGroup_name(groupData.getGroup_name());
                }

                if(groupData.getGroup_avatar()!=null)
                {
                    myGroupArry.get(i).setGroup_avatar(groupData.getGroup_avatar());
                }

                if(groupData.getGroup_saying()!=null)
                {
                    myGroupArry.get(i).setGroup_saying(groupData.getGroup_saying());
                }

                setGroupStage(myGroupArry);

                GroupStage groupStage=Main.AllContronller.getGroupStage(groupData.getGroup_id());
                if(groupStage!=null) groupStage.changeStage(groupData);
                break;
            }
        }
    }

    public void setChatStage(PrivateInformationData mydata, ArrayList<PrivateInformationData> arrayList) {

        this.mydata=mydata;
        System.out.println(" "+mydata.getUserid()+" "+mydata.getEmailNumber()+" "+mydata.getUserNickName());
        this.arry=arrayList;
        System.out.println(arrayList.size());
        privateInformationDataHashMap.clear();
        myid=mydata.getUserid();
        myemail=mydata.getEmailNumber();
        this.mydata=mydata;
        userid.setText("用户id:" + mydata.getUserid());
        NickName.setText("用户昵称:" + mydata.getUserNickName());
        emailNumber.setText("用户邮箱:" + mydata.getEmailNumber());
        if(mydata.getAvatar()!=null)
        {
            Image avatarimage = new Image(new ByteArrayInputStream(mydata.getAvatar()));
            avatar.setFitHeight(100);
            avatar.setFitWidth(100);
            Circle circle=new Circle(50,50,50);
            avatar.setImage(avatarimage);
            avatar.setClip(circle);
        }

        id = mydata.getUserid();

        oblist = FXCollections.observableArrayList();
        UserInfolist.setItems(oblist);

        oblist.setAll(arrayList);
        UserInfolist.refresh();


        //设置单元格
        UserInfolist.setCellFactory(new Callback<ListView<PrivateInformationData>, ListCell<PrivateInformationData>>() {
            @Override
            public ListCell<PrivateInformationData> call(ListView<PrivateInformationData> param) {
                ListCell<PrivateInformationData> list=new ListCell<PrivateInformationData>(){
                    @Override
                    protected void updateItem(PrivateInformationData arg0, boolean arg1) {
                        super.updateItem(arg0, arg1);
                        if(arg1==false){

                            System.out.println(arg0.getUserNickName()+" "+arg0.getIsfriend());
                            if(arg0.getIsfriend()==2)
                            {
                                //需要验证的好友
                                setPrefHeight(70);
                                setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
                                HBox hbox=new HBox(10);
                                hbox.setAlignment(Pos.BASELINE_LEFT);//对齐显示
                                Image image=new Image(new ByteArrayInputStream(arg0.getAvatar()));
                                ImageView imageView=new ImageView(image);
                                Label Id=new Label(arg0.getUserid());
                                imageView.setFitWidth(60);
                                imageView.setFitHeight(60);
                                Circle circle=new Circle(30,30,30);
                                Label la=new Label("请求加你为好友");
                                la.setFont(Font.font(15));
                                imageView.setClip(circle);
                                hbox.getChildren().addAll(imageView,Id,la);//添加
                                this.setGraphic(hbox);//启动

                                putPID(arg0.getUserid(),arg0);
                            }
                            else if(arg0.getIsfriend()==1)
                            {
                                setPrefHeight(70);
                                setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
                                HBox hbox=new HBox(10);
                                hbox.setAlignment(Pos.BASELINE_LEFT);//对齐显示
                                Image image=new Image(new ByteArrayInputStream(arg0.getAvatar()));
                                ImageView imageView=new ImageView(image);
                                Label Id=new Label(arg0.getUserid());
                                imageView.setFitWidth(60);
                                imageView.setFitHeight(60);
                                Circle circle=new Circle(30,30,30);
                                Label la=new Label(arg0.getUserNickName());
                                imageView.setClip(circle);

                                if(arg0.getIsonline()==1)
                                {
                                    //说明用户在线
                                    System.out.println("www");
                                    Image userislone=new Image("file:"+"E:\\JDK\\QQClient\\nox\\src\\img\\online.png");
                                    ImageView online=new ImageView(userislone);
                                    online.setFitHeight(40);
                                    online.setFitWidth(40);
                                    hbox.getChildren().addAll(imageView,la,Id,online);
                                }
                                else hbox.getChildren().addAll(imageView,la,Id);//添加
                                this.setGraphic(hbox);//启动

                                System.out.println(arg0.getUserid());
                                putPID(arg0.getUserid(),arg0);
                            }
                            else
                            {
                                //不是好朋友，不管
                            }
                        }
                        else
                        {
                            setPrefHeight(0);
                        }

                    }

                };
                return list;
            }
        });
        UserInfolist.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<PrivateInformationData>() {
            //s：改变前的值    t1：改变后的值
            @Override
            public void changed(ObservableValue<? extends PrivateInformationData> observableValue, PrivateInformationData s, PrivateInformationData t1) {

            //    UserInfolist.getSelectionModel().clearSelection();
                if(t1!=null&&t1.getIsfriend()==2)
                {
                    VerifyFriends old=Main.AllContronller.getVerifyFriendStagById(t1.getUserid());
                    if(old==null)
                    {
                        //如果不是朋友，需要加载另外一个控制类
                        Stage stage=new Stage();
                        File file=new File("E:\\JDK\\QQClient\\nox\\src\\fxml\\verify Friends.fxml");
                        URL location= null;
                        try {
                            location = file.toURI().toURL();
                        } catch (MalformedURLException e) {
                            throw new RuntimeException(e);
                        }
                        FXMLLoader fxmlLoader=new FXMLLoader();
                        fxmlLoader.setLocation(location);
                        Parent root= null;
                        try {
                            root = fxmlLoader.load();
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                        stage.setTitle("好友请求");
                        stage.setResizable(false);
                        stage.setScene(new Scene(root,400,400));
                        stage.show();

                        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                            @Override
                            public void handle(WindowEvent event) {
                                Main.AllContronller.RemoveVerifyFriendStage(t1.getUserid());
                                stage.close();
                            }
                        });
                        VerifyFriends verifyFriends=fxmlLoader.getController();
                        Main.AllContronller.AddVerifyFriendStage(t1.getUserid(),verifyFriends);
                        System.out.println(t1.getUserid()+t1.getUserNickName());
                        verifyFriends.setPrivateInformationData(t1);
                    }
                }
                else if(t1!=null&&t1.getIsfriend()==1)
                {
                    Stage stage =new Stage();
                    File file=new File("E:\\JDK\\QQClient\\nox\\src\\fxml\\ChatWithStage.fxml");
                    URL location= null;
                    try {
                        location = file.toURI().toURL();
                    } catch (MalformedURLException e) {
                        throw new RuntimeException(e);
                    }
                    FXMLLoader fxmlLoader=new FXMLLoader();
                    fxmlLoader.setLocation(location);
                    Parent root= null;
                    try {
                        root = fxmlLoader.load();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }

                    ChatWithStage chatWithStage=fxmlLoader.getController();
                    chatWithStage.setSocket(socket);
                    Main.getAllContronller().AddChatWithStage(t1.getUserid(),chatWithStage);
                    System.out.println("放入了控制类"+ t1.getUserid());
                    ChatData chatData=new ChatData(mydata.getAvatar(),t1.getAvatar(),mydata.getUserid(),t1.getUserid());
                    chatWithStage.setChatData(chatData);

                    chatWithStage.setCommonSentence(sentenceList);

                    if(map.containsValue(chatData))
                    {
                        return ;
                    }
                    map.put(chatWithStage,chatData);

                    stage.setTitle(t1.getUserNickName());
                    stage.setResizable(false);
                    stage.setScene(new Scene(root,600,500));
                    stage.show();

                    stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                        @Override
                        public void handle(WindowEvent event) {
                            Main.AllContronller.RemoveChatWithStage(t1.getUserid());
                            System.out.println("此窗口已经关闭");
                            map.remove(fxmlLoader.getController());
                            //    System.out.print("监听到窗口关闭");
                        }
                    });

                    if(MsgList.isExistId(t1.getUserid()))
                    {
                        //如果已经存在消息,不需要请求
                        System.out.println(t1.getUserNickName());
                        for (int i = 0; i < MsgList.getMassageList(t1.getUserid()).getMassagelist().size(); i++) {
                            System.out.println(MsgList.getMassageList(t1.getUserid()));
                        }
                        chatWithStage.setChatData(chatData);
                        chatWithStage.setStage(MsgList.getMassageList(t1.getUserid()).getMassagelist());
                    }
                    else
                    {
                        System.out.println("发送需要聊天记录的消息");
                        chatWithStage.setSocket(socket);
                        chatWithStage.setChatData(chatData);
                        try {
                            event massage=new event();
                            massage.setType(MassageType.Request_Chat_Massage);
                            Timestamp timestamp=new Timestamp(System.currentTimeMillis());
                            MassageData massageData=new MassageData(mydata.getUserid(),t1.getUserid(),timestamp,"");
                            massageData.setSum(0);
                            massage.setT(massageData);

                            new SendMassage().sendMassage(massage);

                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }

                        System.out.println("发送成功");
                    }
                }
                else return;
            }
        });

        UserInfolist.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(event.getButton().equals(MouseButton.PRIMARY))
                {
                    if(event.getClickCount()==1)
                    {
                        UserInfolist.getSelectionModel().clearSelection();
                     //   System.out.println("双击");
                    }
                }
            }
        });

    }

    public ArrayList<GroupData> myGroupArry;

    public void ExitGroup(GroupData groupData)
    {
        for (int i = 0; i < myGroupArry.size(); i++) {
            if(myGroupArry.get(i).getGroup_id().equals(groupData.getGroup_id()))
            {
                myGroupArry.remove(i);
                break;
            }
        }
        GroupStage groupStage=Main.AllContronller.getGroupStage(groupData.getGroup_id());
        if(groupStage!=null) groupStage.CloseStage();
        Main.AllContronller.RemoveGroupStage(groupData.getGroup_id());
        setGroupStage(myGroupArry);
    }

    public void changeGroup(GroupData old,GroupData now)
    {
        for (int i = 0; i < myGroupArry.size(); i++) {
            if(myGroupArry.get(i).getGroup_id().equals(old.getGroup_id()))
                myGroupArry.remove(i);
        }
        myGroupArry.add(now);
        setGroupStage(myGroupArry);
    }

    public void AddGroup(GroupData groupData)
    {
        myGroupArry.add(groupData);
        System.out.println("加入群聊成功");
        setGroupStage(myGroupArry);
    }

    public HashMap<String,ArrayList<GroupFriendData>> friendListDataHashMap=new HashMap<>();

    public void AddGroupFriendList(String group_id,ArrayList<GroupFriendData> arry)
    {
        friendListDataHashMap.put(group_id,arry);
        for (int i = 0; i < myGroupArry.size(); i++) {
            if(myGroupArry.get(i).getGroup_id().equals(group_id))
            {
                GroupData groupData=myGroupArry.get(i);
                groupData.setGroup_friend_list(arry);
                myGroupArry.remove(i);
                myGroupArry.add(groupData);
            }
        }
    }



    public void ApendGroupFriendListById(String group_id,GroupFriendData groupFriendData)
    {
        ArrayList<GroupFriendData> list=friendListDataHashMap.get(group_id);
        if(list!=null)
        {
            list.add(groupFriendData);
            friendListDataHashMap.put(group_id,list);
        }
    }


    public void setGroupStage(ArrayList<GroupData> group_Arry)
    {
        myGroupArry=group_Arry;
        Group_oblist=FXCollections.observableArrayList();
        Group_list.setItems(Group_oblist);

        Group_oblist.setAll(group_Arry);
        Group_list.refresh();
        System.out.println("群聊个数为"+group_Arry.size());
        Group_list.setCellFactory(new Callback<ListView<GroupData>, ListCell<GroupData>>() {
            @Override
            public ListCell<GroupData> call(ListView<GroupData> param) {
                ListCell<GroupData> list=new ListCell<GroupData>(){
                    @Override
                    protected void updateItem(GroupData arg0, boolean arg1) {
                        super.updateItem(arg0, arg1);
                        if(arg1==false)
                        {
                            System.out.println(arg0.getGroup_id()+" "+arg0.getId()+(arg0.getGroup_avatar()==null));
                            setPrefHeight(70);
                            setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
                            HBox hbox=new HBox(10);
                            hbox.setAlignment(Pos.BASELINE_LEFT);
                            Image image;
                            //群聊，需要显示头像数据和名称和群聊id
                            if(arg0.getGroup_avatar()==null)
                            {
                                //还没有设置图片，加载默认图片吧
                                image=new Image("file:"+"E:\\JDK\\QQClient\\nox\\src\\img\\2\\1.jpeg");
                            }
                            else
                            {
                                image=new Image(new ByteArrayInputStream(arg0.getGroup_avatar()));
                            }
                            ImageView imageView=new ImageView(image);
                            imageView.setFitWidth(60);
                            imageView.setFitHeight(60);
                            Circle circle=new Circle(30,30,30);
                            Label id=new Label(arg0.getGroup_id());
                            Label nickname;
                            if(arg0.getGroup_name()!=null) nickname=new Label(arg0.getGroup_name());
                            else nickname=new Label("");
                            imageView.setClip(circle);

                            hbox.getChildren().addAll(imageView,nickname,id);
                            this.setGraphic(hbox);
                        }
                        else
                        {
                            setPrefHeight(0);
                        }

                    }

                };
                return list;
            }
        });
        Group_list.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<GroupData>() {
            //s：改变前的值    t1：改变后的值
            @Override
            public void changed(ObservableValue<? extends GroupData> observableValue, GroupData s, GroupData t1) {
                if(t1!=null&&t1.getIdentity()!=0)
                {
                    System.out.println(Main.AllContronller.getChatMainController()==null);
                    if(Main.AllContronller.getGroupStage(t1.getGroup_id())!=null) return ;
                    Stage stage=new Stage();
                    File file=new File("E:\\JDK\\QQClient\\nox\\src\\fxml\\group_Stage.fxml");
                    URL location=null;
                    try {
                        location=file.toURI().toURL();
                    } catch (MalformedURLException e) {
                        throw new RuntimeException(e);
                    }
                    FXMLLoader fxmlLoader=new FXMLLoader();
                    fxmlLoader.setLocation(location);
                    Parent root=null;
                    try {
                        root=fxmlLoader.load();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }

                    GroupStage groupStage=fxmlLoader.getController();
                    Main.AllContronller.AddGroupStage(t1.getGroup_id(),groupStage);
                    System.out.println(t1.getGroup_id()+"已经放入map中");
                    if(t1.getIdentity()==1)
                    {
                        groupStage.setGroupOwner(t1);
                    }
                    else if(t1.getIdentity()==2)
                    {
                        groupStage.setAdmin(t1);
                    }
                    else if(t1.getIdentity()==3)
                    {
                        groupStage.setPeople(t1);
                    }

                    groupStage.setMyavatar(new Image(new ByteArrayInputStream(mydata.getAvatar())));
                    System.out.println("我的身份为"+t1.getIdentity());
                    stage.setTitle(t1.getGroup_name());
                    stage.setResizable(false);
                    stage.setScene(new Scene(root,800,500));
                    stage.show();

                    if(!friendListDataHashMap.containsKey(t1.getGroup_id()))
                    {
                        //需要好友列表，向服务器发送请求
                        event massage=new event();
                        massage.setType(MassageType.Request_Get_Friend_list_From_Group);
                        massage.setT(t1.getGroup_id());

                        try {
                            new SendMassage().sendMassage(massage);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    else
                    {
                        groupStage.setAllFriend(friendListDataHashMap.get(t1.getGroup_id()));
                    }

                    if(MsgList.isExistId(t1.getGroup_id()))
                    {
//                        for (int i = 0; i < MsgList.getMassageList(t1.getGroup_id()).getMassagelist().size(); i++) {
//                         //   System.out.println();
//                        }
                        t1.setMassageListData(MsgList.getMassageList(t1.getGroup_id()));
                        t1.setId(myid);


                        groupStage.setMassage(MsgList.getMassageList(t1.getGroup_id()).getMassagelist());
                    }
                    else
                    {
                        System.out.println("群聊需要聊天记录");
                        event massage=new event();
                        massage.setType(MassageType.Request_Get_More_Massage_From_Group);
                        MassageData massageData=new MassageData();
                        massageData.setSender(t1.getId());
                        massageData.setReceiver(t1.getGroup_id());
                        massageData.setSum(0);
                        massage.setT(massageData);

                        t1.setId(myid);
                        try {
                            new SendMassage().sendMassage(massage);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    groupStage.setData(t1);
                    groupStage.setInitGroup();

                    stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                        @Override
                        public void handle(WindowEvent event) {
                            Main.AllContronller.RemoveGroupStage(t1.getGroup_id());
                            System.out.println("群聊"+t1.getGroup_id()+"已经被关闭");
                            //    System.out.print("监听到窗口关闭");
                        }
                    });
                }
                else
                {
                    return;
                }
            }
        });

        Group_list.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(event.getButton().equals(MouseButton.PRIMARY))
                {
                    if(event.getClickCount()==1)
                    {
                        System.out.println("单击");
                        Group_list.getSelectionModel().clearSelection();
                    }
                }
            }
        });

    }

    @FXML
 void initialize() {

 }

 public void deleteGroupFriend(GroupFriendData groupFriendData)
 {
     String group_id=groupFriendData.getGroup_id();
     for (int i = 0; i < myGroupArry.size(); i++)
     {
         if(myGroupArry.get(i).getGroup_id().equals(group_id))
         {
             //是当前的群聊
             ArrayList<GroupFriendData> arrayList=myGroupArry.get(i).getGroup_friend_list();
             if(arrayList!=null)
             {
                 for (int j = 0; j < arrayList.size(); j++)
                 {
                     if(arrayList.get(j).getId().equals(groupFriendData.getId()))
                     {
                         //如果id相同，则删除
                         arrayList.remove(j);
                         myGroupArry.get(i).setGroup_friend_list(arrayList);

                         GroupStage groupStage=Main.AllContronller.getGroupStage(groupFriendData.getGroup_id());
                         if(groupStage!=null)
                         {
                             groupStage.setAllFriend(arrayList);
                         }
                         break;
                     }
                 }

             }
             break;
         }
     }
 }

 public void changeGroupIdentity(String group_id,int identity)
 {
     System.out.println("来修改自己的群聊身份了");
     //先找到对应的身份，改变身份的函数
     for (int i = 0; i < myGroupArry.size(); i++) {
         if(myGroupArry.get(i).getGroup_id().equals(group_id))
         {
             //需要修改身份信息
             myGroupArry.get(i).setIdentity(identity);

             ArrayList<GroupFriendData> arrayList=myGroupArry.get(i).getGroup_friend_list();
             if(arrayList!=null)
             {
                 for (int j = 0; j < arrayList.size(); j++) {
                     if(arrayList.get(j).getId().equals(myGroupArry.get(i).getId()))
                     {
                         //此时好友列表的自己，需要修改
                         System.out.println("修改好友列表的自己的身份"+arrayList.get(j).getIdentity());
                         arrayList.get(j).setIdentity(identity);
                         System.out.println("看看我修改成功了吗，此时我的身份是"+arrayList.get(j).getIdentity());
                     }
                 }
                 myGroupArry.get(i).setGroup_friend_list(arrayList);
             }
             //然后需要改变对应的窗口显示
             GroupStage groupStage=Main.AllContronller.getGroupStage(group_id);
             if(groupStage!=null)
             {
                 //说明有这个窗口，需要更改该窗口的显示
                 if(identity==1)
                 {
                     //此时是群主
                     groupStage.setGroupOwner(myGroupArry.get(i));
                 }
                 else if(identity==2)
                 {
                     //此时是管理员
                     groupStage.setAdmin(myGroupArry.get(i));
                 }
                 else
                 {
                     //此时是群友
                     groupStage.setPeople(myGroupArry.get(i));
                 }
                 //此时已经修改好了界面的显示，然后就是好友列表的更新
                 //好友列表自己的身份需要改变,先得到对应群聊的好友列表
                 //重新设置了列表
                 groupStage.setAllFriend(arrayList);
             }
             else
             {
                 System.out.println("此时"+group_id+"控制类为空");
             }
             break;
         }
     }
 }

 public void changeGroupFriendListIdentity(ArrayList<GroupFriendData> arry)
 {
     //先找到对应的群聊信息
     String group_id=arry.get(0).getGroup_id();
     System.out.println("群聊id为"+group_id);
     for (int i = 0; i < myGroupArry.size(); i++) {
         if(myGroupArry.get(i).getGroup_id().equals(group_id))
         {
             //找到了对应的群聊
             System.out.println("找到了对应的群聊");
             ArrayList<GroupFriendData> arrayList=myGroupArry.get(i).getGroup_friend_list();
             if(arrayList!=null)
             {
                 for (int j = 0; j < arrayList.size(); j++) {
                     for (int k = 0; k < arry.size(); k++) {
                         if(arrayList.get(j).getId().equals(arry.get(k).getId()))
                         {
                             //如果当前好友是需要改变的信息，则改变
                             System.out.println("改变"+arrayList.get(j).getId()+"的身份,原来的身份为"+arrayList.get(j).getIdentity());
                             arrayList.get(j).setIdentity(arry.get(k).getIdentity());
                             System.out.println("看看我是否更改成功"+arrayList.get(j).getIdentity());
                         }
                     }
                 }
                 //找到对应的群聊之后，需要改变群聊的好友列表的信息
                 myGroupArry.get(i).setGroup_friend_list(arrayList);
             }
             GroupStage groupStage=Main.AllContronller.getGroupStage(group_id);
             if(groupStage!=null)
             {
                 groupStage.setAllFriend(arrayList);
             }
         }
     }
 }

 public ResourceBundle getResources() {
  return resources;
 }

    public void ChangeInformation(MouseEvent mouseEvent) throws IOException {
        PrivateInformation privateInformation= Main.AllContronller.getPrivateInformationController();
        if(privateInformation!=null)
        {
            System.out.println("控制类不为空");
            return;
        }
        Stage stage=new Stage();
        File file=new File("E:\\JDK\\QQClient\\nox\\src\\fxml\\PrivateInformation.fxml");
        URL location=file.toURI().toURL();
        FXMLLoader fxmlLoader=new FXMLLoader();
        fxmlLoader.setLocation(location);
        Parent root=fxmlLoader.load();
        stage.setTitle("个人信息修改");
        stage.setResizable(false);
        stage.setScene(new Scene(root,400,600));
        stage.show();

        Platform.runLater(() -> {
            PrivateInformation nox=fxmlLoader.getController();
            nox.setSocket(socket);
            Main.AllContronller.setPrivateInformationController(nox);
            nox.changeInformation(mydata);
        });
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                Main.AllContronller.setPrivateInformationController(null);

                System.out.print("监听到个人信息的窗口关闭");
                stage.close();
            }
        });
    }

    public void AddFriendAction(ActionEvent actionEvent) throws IOException {
        AddFriend addFriend= Main.AllContronller.getAddFriendController();
        if(addFriend!=null)
        {
            return ;
        }
        Stage stage=new Stage();
        File file=new File("E:\\JDK\\QQClient\\nox\\src\\fxml\\AddFriend.fxml");
        URL location=file.toURI().toURL();
        FXMLLoader fxmlLoader=new FXMLLoader();
        fxmlLoader.setLocation(location);
        Parent root=fxmlLoader.load();
        stage.setTitle("添加好友");
        stage.setResizable(false);
        stage.setScene(new Scene(root,400,400));
        stage.show();
        Main.AllContronller.setAddFriendController(addFriend);

        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                stage.close();
                Main.AllContronller.setAddFriendController(null);
            }
        });

        Main.AllContronller.setAddFriendController(fxmlLoader.getController());
        addFriend= Main.AllContronller.getAddFriendController();
        System.out.println(myid);
        addFriend.setMy(myid,myemail);
    }


    public void AddGroup(ActionEvent actionEvent) throws IOException {
        //添加群聊
        AddFriend addToGroup= Main.AllContronller.getAddgroup();
        if(addToGroup!=null)
        {
            return ;
        }
        Stage stage=new Stage();
        File file=new File("E:\\JDK\\QQClient\\nox\\src\\fxml\\AddFriend.fxml");
        URL location=file.toURI().toURL();
        FXMLLoader fxmlLoader=new FXMLLoader();
        fxmlLoader.setLocation(location);
        Parent root=fxmlLoader.load();
        stage.setTitle("加入群聊");
        stage.setResizable(false);
        stage.setScene(new Scene(root,400,400));
        stage.show();
        Main.AllContronller.setAddFriendController(addToGroup);

        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                stage.close();
                Main.AllContronller.setAddgroup(null);
            }
        });

        Main.AllContronller.setAddgroup(fxmlLoader.getController());
        addToGroup= Main.AllContronller.getAddgroup();
        addToGroup.setMy(myid,null);
        addToGroup.setAddGroup();
    }

    public void CreatGroupAction(ActionEvent actionEvent) throws IOException {
        //创建群聊，分配一个id
        //创建成功之后需要显示到列表
        event massage=new event();
        massage.setType(MassageType.Creat_Group);
        massage.setT(new String(myid));
        new SendMassage().sendMassage(massage);

    }

    public void addSentence(CommonSentenceData commonSentenceData)
    {
        sentenceList.add(commonSentenceData.getCommomsentence());
        for (int i = 0; i < arry.size(); i++) {
            ChatWithStage chatWithStage=Main.AllContronller.getChatWithStageById(arry.get(i).getUserid());
            if(chatWithStage!=null)
            {
                chatWithStage.setCommonSentence(sentenceList);
            }
        }
    }


    public void deleteSentence(CommonSentenceData commonSentenceData)
    {
        sentenceList.remove(commonSentenceData.getCommomsentence());
        System.out.println(commonSentenceData.getUserid());
        for (int i = 0; i < arry.size(); i++) {
            ChatWithStage chatWithStage=Main.AllContronller.getChatWithStageById(arry.get(i).getUserid());
            if(chatWithStage!=null)
            {
                chatWithStage.setCommonSentence(sentenceList);
            }
        }
    }

    public static ArrayList<String> sentenceList;

    public void setCommonSentence(ArrayList<String> arrayList) {
        ChatMain.sentenceList=arrayList;
    }
}
